$(function(){
	var headHTML="";
	var curData=JSON.parse(sessionStorage.getItem('curData'));
	/* 获取用户信息 */
	
	ym.ajax({
		url: "/user/getUser",
		success: function (data) {
		   if (data.obj) {
		   	sessionStorage.setItem('curData',JSON.stringify(data.obj));
		   	headHTML+="<div class='form-list'>"+
		   				"<div id='headimgurl'>"+
							  "<form enctype='multipart/form-data'>"+
							  "<div class='headerdivs'>"+
							  	"<div id='preview'>"+
							   		"<img src='"+curData.headimgurl+"' id='headerImg' />"+
								"</div>"+
								"</div>"+
							"<input type='file' id='file' name='file' accept='image/*'  onchange='preview(this)' />"+
							"<div class='userName1'><p>更改头像</p></div>"+
						  	"</form>"+
						"</div>"+
					  "<ul class='am-list'>"+
					  "<li>"+
					  "<a class='w100' href='javascript:void(0)'>"+
					  "<p>昵称：</p>"+
					  "<p class='w90'><input type='text' class='userName' style='border: none;outline: none;width:80%' value='"+curData.nickname+"'/></p>"+
					  "</a>"+
					  "</li>"+
					 /* "<li>"+
					  "<a href='#'>"+
					  "<p>签名</p>"+
					  "<p><i class='am-icon-angle-right'></i></p>"+
					  "</a>"+
					  "</li>"+*/
					  "</ul>"+
				 	  "</div>"+
				 	  "<div class='form-list'>"+
				 	  "<ul class='am-list'>"+
					  "<li id='changePhone'>"+
					  "<a href='../html/addPhone.html'>"+
					  "<p>手机号码："+curData.userPhone+"</p>"+
					  "<p>"+
					  "<i class='am-icon-angle-right'></i>"+
					  "</p>"+
					  "</a>"+
					  "</li>"+
					  "</ul>"+
				      "</div>";
			$('.content').html(headHTML);
		   }
		}
	});
	window.setTimeout(function(){
		$('.userName').blur(function(){
			if($(this).val() == '' || $(this).val() == null ||$(this).val() == undefined){
                $('#ceng').modal('open');
                $('#info-text').html('不能改为空哦！');
                window.setTimeout(function(){
                    $('#ceng').modal('close');
                },1000)
                $(this).val(curData.nickname);
			}else{
                var jsonData={
                    nickname:$(this).val(),
                    userId:curData.userId
                }
                ym.ajax({
                    url: "/user/updateUser",
                    param:jsonData,
                    success: function (data) {
                        $('#ceng').modal('open');
                        $('#info-text').html('修改成功！');
                        window.setTimeout(function(){
                            $('#ceng').modal('close');
                        },1000)
                    }
                });
			}
	})
	},1500)
	/*完成编辑*/
	$('#complet').click(function(){
		window.location.href='../html/personal.html'
	})

})
//图片预览
function preview(obj) {
	console.log("点击");
    var url= window.URL.createObjectURL(obj.files[0]);
    console.log(obj.files[0])
    $("#headerImg").attr("src",url);
    imgUpload(obj.files[0]);
}
/*上传图片*/
function imgUpload(file){
    var formData=new FormData();
    formData.append("imageFile",file);
    formData.append("relevanceId",1);
    formData.append("relevanceTpyeId",4);
    $.ajax({
        url: "/web/uploadUserHeader",
        type : "post",
        processData : false,
        contentType : false,
        data:formData,
        success : function(data) {
            ym.ajax({
			url: "/user/updateUser",
            type : "post",
			param:{
                headimgurl:data.obj.imagePath
			},
			success: function (data) {
			   $('#ceng').modal('open');
			   $('#info-text').html('修改成功！');
			   window.setTimeout(function(){
			   	 $('#ceng').modal('close');
			   },500)
			},error: function(data) {
				}
            });
		}
    });
}